package com.goldplusgold.tradedelay.app.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goldplusgold.support.lib.base.adapter.AdapterParentBase;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.support.lib.hybird.WebViewActivity;
import com.goldplusgold.tradedelay.app.home.pojo.Article;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ArticleAdapter extends AdapterParentBase<Article> implements AdapterView.OnItemClickListener {
    public ArticleAdapter(Context ctx) {
        super(ctx);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_home_expert_comment, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Article article = getItem(position);
        if (article != null) {
            holder.idTextViewTitle.setText(article.getTitle());
            // Glide.with(mContext).load(article.getImageUrl()).transform(new GlideCircleTransform(mContext)).thumbnail(0.1f).into(holder.idImageViewHead);
            holder.idTextViewUserName.setText(article.getNickname());
            holder.createTimeTxt.setText(article.getIssuanceTime());
            holder.mediaContent.setText(article.getSummary());
            Glide.with(mContext).load(article.getImageUrl()).thumbnail(0.5f).into(holder.mediaImage);
        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Article article = getItem(position);
        if (article == null) {
            return;
        }
        Intent intent = new Intent(getContext(), WebViewActivity.class);
        intent.putExtra("url", article.getLinkUrl());
        getContext().startActivity(intent);
    }


    public static class ViewHolder {
        @BindView(R.id.id_text_view_title)
        TextView idTextViewTitle;
        @BindView(R.id.id_image_view_head)
        ImageView idImageViewHead;
        @BindView(R.id.id_text_view_user_name)
        TextView idTextViewUserName;
        @BindView(R.id.create_time_txt)
        TextView createTimeTxt;
        @BindView(R.id.media_image)
        ImageView mediaImage;
        @BindView(R.id.media_content)
        TextView mediaContent;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
